# -*- coding: utf-8 -*-
from skimage.color import rgb2gray
from skimage.io import imread
from scipy import signal
import numpy as np
import scipy.fftpack as fp
from scipy import misc
from matplotlib import pylab
import timeit


#比较scipy convolve()和fftconvolve()与高斯核的运行时间
im=np.mean(imread('mandrill.jpg'),axis=2)
gauss_kernel=np.outer(signal.gaussian(11,3),signal.gaussian(11,3))#定义二维高斯模板11X11
im_blurred1=signal.convolve(im,gauss_kernel,mode='same')#进行卷积
im_blurred2=signal.fftconvolve(im,gauss_kernel,mode='same')#进行快速傅里叶卷积
def wrapper_convolve(func):
    def wrapped_convolve():
        return func(im,gauss_kernel,mode='same')
    return wrapped_convolve
wrapped_convolve=wrapper_convolve(signal.convolve)
wrapped_fftconvolve=wrapper_convolve(signal.fftconvolve)
time1=timeit.repeat(wrapped_convolve,number=1,repeat=100)
time2=timeit.repeat(wrapped_fftconvolve,number=1,repeat=100)
    
#显示图像结果
pylab.figure(figsize=(15,5))
pylab.gray()
pylab.subplot(131)
pylab.imshow(im)
pylab.title('原始图像',size=15)
pylab.axis('off')

pylab.subplot(132)
pylab.imshow(im_blurred1)
pylab.title('卷积输出图像',size=15)
pylab.axis('off')

pylab.subplot(133)
pylab.imshow(im_blurred2)
pylab.title('快速傅里叶卷积输出图像',size=15)
pylab.axis('off')
pylab.show()








